package com.mmall.seckill.common;

import lombok.Data;

/**
 * Created by megan on 2018/6/15.
 * 服务端响应工具类
 */
@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    private Result(int code) {
        this.code = code;
    }

    private Result(int code, T data) {
        this.code = code;
        this.data = data;
    }

    private Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(CodeMsg codeMsg) {
        if(codeMsg == null){
            return;
        }
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
    }

    public enum ResultEnumCode{
        RESULT_SUCCESS(0 ,"SUCCESS"), RESULT_ERROR(1,"ERROR"), SERVER_ERROR(500100,"服务端异常"), BIND_ERROR(500101,"参数校验异常：%s"),ACCESS_LIMIT_REACHED(500102,"访问太频繁"),
        SESSION_ERROR(500210,"Session不存在或者已经失效"), PASSWORD_EMPTY(500211,"登录密码不能为空"),PASSWORD_ERROR(500215,"密码错误"), MOBILE_EMPTY(500212,"手机号不能为空"),
        MOBILE_ERROR(500213,"手机号格式错误"), MOBILE_NOT_EXIST(500214,"手机号不存在"), MIAO_SHA_OVER(500500,"商品已经秒杀完毕"),REPEATE_MIAOSHA(500501,"不能重复秒杀"),
        ORDER_NOT_EXIST(500400,"没有这个订单"),AWAIT_MIAOSHA(500502,"秒杀中排队中，请稍后。。。。"),MIAOSHA_FAIL(500503,"秒杀失败"),REQUEST_ILLEGAL(500504,"非法请求"),
        VERIFYCODE_FAILURE(500505,"验证码失效，请刷新"),VALIDATION_FAILS(500506,"验证码验证失败，请重试");

        private final int code;
        private final String desc;

        ResultEnumCode(int code,String desc){
            this.code=code;
            this.desc=desc;
        }
        public int getCode(){
            return  code;
        }
        public String getDesc(){
            return desc;
        }
    }

    public Boolean isSuccess(){
        return this.code == ResultEnumCode.RESULT_SUCCESS.getCode();
    }

    public static<T> Result<T> success(T data){
        return  new Result<T>(ResultEnumCode.RESULT_SUCCESS.getCode(),data);
    }
    public static<T> Result<T> success(){
        return  new Result<T>(ResultEnumCode.RESULT_SUCCESS.getCode());
    }

    public static<T> Result<T> error(){
        return new Result<T>(ResultEnumCode.RESULT_ERROR.getCode());
    }

    public static <T> Result <T> error(String msg){
        return new Result<T>(ResultEnumCode.RESULT_ERROR.getCode(),msg);
    }

    public static <T> Result <T> error(int code, String msg){
        return new Result<T>(code,msg);
    }

    public static <T> Result <T> coderMsg(CodeMsg codeMsg){
        return  new Result<T>(codeMsg);
    }

}
